<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\ItemImage
 *
 * @property int $id
 * @property int $itemid
 * @property string $thumb
 * @property string $image
 * @property int $displayorder
 * @property-read \App\Models\Item $item
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage whereDisplayorder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage whereItemid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ItemImage whereThumb($value)
 * @mixin \Eloquent
 */
class ItemImage extends Model
{
    protected $table = 'item_image';
    protected $primaryKey = 'id';
    protected $casts = [
        'displayorder' => 'integer'
    ];
    protected $fillable = [
        'itemid', 'thumb', 'image', 'displayorder'
    ];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::addGlobalScope('sort', function (Builder $builder) {
            return $builder->orderBy('displayorder')->orderBy('id');
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getThumbAttribute($value)
    {
        return $value ? image_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setThumbAttribute($value)
    {
        $this->attributes['thumb'] = strip_image_url($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getImageAttribute($value)
    {
        return $value ? image_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setImageAttribute($value)
    {
        $this->attributes['image'] = strip_image_url($value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function item()
    {
        return $this->belongsTo(Item::class, 'itemid', 'itemid');
    }
}
